<template>
  <div>
    <el-tree
      ref="refTree"
      :data="perList"
      show-checkbox
      node-key="id"
      default-expand-all
      check-strictly
      :default-checked-keys="permIds"
      :props="defaultProps"
    />
    <div>
      <el-button size="small">取消</el-button>
      <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
    </div>
  </div>
</template>
<script>
import { getPermissionList } from '@/api/permissions'
import { getRoleDetail, assignPerm } from '@/api/setting'
import arrayToTree from 'array-to-tree'
export default {
  name: '',
  props: ['roleId'],
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      perList: [],
      permIds: []
    }
  },
  computed: {
  },
  created() {
    this.loadPermissionList()
    this.getRight()
  },
  methods: {
    async loadPermissionList() {
      const res = await getPermissionList()
      // console.log(res)
      //   this.list = res.data
      this.perList = arrayToTree(res.data, {
        parentProperty: 'pid',
        customID: 'id'
      })
    },
    async getRight() {
      const res = await getRoleDetail(this.roleId)
      this.permIds = res.data.permIds
    },
    async hSubmit() {
      var prtMissIds = this.$refs.refTree.getCheckedKeys()
      const res = await assignPerm({
        id: this.roleId,
        permIds: prtMissIds
      })
      console.log(res)
      this.$emit('success2')
    }
  }
}
</script>
<style lang='less'  scoped>

</style>
